/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package wwcontrolserver;

import com.phidgets.*;
import java.util.Date;

/**
 *
 * @author Michael Ferry
 */
public class ControlFunctionsServer {

    public static void HeaterControl(int InputSensorNumber, InterfaceKitPhidget HeaterSensorIFK,
            int OutputRelayNumber, InterfaceKitPhidget RelayOutputIFK, double TempSetPoint, double TempSetRange) {

        int CurrentRawSensor;
        double CurrentBarrelTemperature;

        //for (int i = 0; i < WWControlServer.HeaterSensorMap.length; i++) {
        //System.out.println(SensorOutputMap.length);
        //Determine if the current barrel is under temperature control

        try {
            if (HeaterSensorIFK.isAttached() && RelayOutputIFK.isAttached()) {
                CurrentRawSensor = HeaterSensorIFK.getSensorRawValue(InputSensorNumber);
                CurrentBarrelTemperature = SensorComputationsServer.ComputeTemperature2252ohms(CurrentRawSensor);
                //If the barrel temperature is below the TempSet point minus the TempSetRange turn the heater on
                //System.out.println("CurrTemp, barrel " + i +" "+ CurrentBarrelTemperature + " TempSet " + TempSetPoint + " TempSetRange" + TempSetRange);
                if (CurrentBarrelTemperature < TempSetPoint - TempSetRange) {
                    //System.out.println("Turning on heater: "+ SensorOutputMap[i]);
                    RelayOutputIFK.setOutputState(OutputRelayNumber, true);
                } //If the barrel temperature is above the TempSet point minus the TempSetRange turn the heater off
                else if (CurrentBarrelTemperature > TempSetPoint + TempSetRange) {
                    //System.out.println("Turning off heater: "+ SensorOutputMap[i]);
                    RelayOutputIFK.setOutputState(OutputRelayNumber, false);
                } else {
                    //System.out.println("No change in heater state");
                }
            } else {
                //Date curTime = new Date();
                System.out.println("Heater component(s) not attached at :" + new Date());
            }
            //Note if the barrel temperature is between TempSetPoint - TempSetRange and TempSetPoint + TempSetRange
            //No action will be taken.  This is a simple hystersis to prevent the heaters from being continually 
            //switched on and off

        } catch (PhidgetException ex) {
            System.out.println(ex);
            //JOptionPane.showMessageDialog(this, ex.getDescription(), "Phidget Error" + ex.getErrorNumber(), JOptionPane.ERROR_MESSAGE);
            //dispose();
            //System.exit(0);
        }
    }
}
